# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=librdkafka
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.2
pkgrel=1
pkgdesc="The Apache Kafka C/C++ client library (mingw-w64)"
arch=('any')
url="https://github.com/edenhill/librdkafka"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/edenhill/librdkafka/archive/v${pkgver}.tar.gz")
sha256sums=('ca3db90d04ef81ca791e55e9eed67e004b547b7adedf11df6c24ac377d4840c6')

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release" \
    -DWITHOUT_WIN32_CONFIG=OFF \
    -DWITH_LIBDL=ON \
    -DWITH_PLUGINS=ON \
    -DWITH_SASL=ON \
    -DWITH_SSL=ON \
    -DWITH_ZLIB=ON \
    "../${_realname}-${pkgver}"

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  cp -f ./src/librdkafka.dll ./src-cpp/librdkafka++.dll ./tests
  ctest -R RdKafkaTestBrokerLess
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  # remove reference to MSYS2 install directory
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/RdKafka/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
